home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 June: Reference Library / Dev.CD Jun 94.toast / Periodicals / develop / develop Issue 15 / develop 15 code / Symmetry & Tiles / Tiler Code / TileInit.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-09-13  |  5.5 KB  |  206 lines  |  [TEXT/KAHL]

  1. #include     <graphics libraries.h>
  2. #include     <graphics toolbox.h>
  3. #include     <offscreen library.h>
  4. #include     <qd library.h>
  5. #include     <GestaltEqu.h>
  6. #include     <graphics debugging.h>
  7. #include    "TileProtos.h"
  8. #include    "TileConstants.h"
  9.  
  10. #define    kGiveMeValidation    false    /* set to TRUE, for run-time validation  */
  11. #define    kDebugging            true   /* set to TRUE, for error & notice posting. This 
  12.                                     functionality is only available with the "debugging"
  13.                                     Secret Graphics init. */
  14.  
  15.  
  16.  
  17. // Globals only needed in this file
  18. WindowRecord        gWindRecord;
  19. Rect                 gWindowQDRect = {50, 50, 306, 306};
  20.  
  21.  
  22. // Other app globals
  23. extern MenuHandle    gAppMenuHandle;
  24. extern WindowPtr    gWindPtr;
  25. extern gxGraphicsClient     gGraphicsClient;
  26.  
  27. //---------------------------------------
  28. //    Tile globals
  29. extern gxPoint        gOrigin;
  30. extern gxColorSet    gColors;
  31. extern offscreen    gOffScreen;
  32. extern gxShape        gTileShape,    // The contours that fill the unit cell (transforms of gDragger.snake1)
  33.                     gGridShape,    // A rectangle filled with the pattern
  34.                     gBGShape;    // A rectangle filled with the background color
  35.                 
  36. extern dragger        gDragger;    // A record containing manipulable contour(s)
  37. extern gxPatternRecord gPattern;
  38.  
  39. extern Boolean            gContRedraw, gShowVectors; // State Booleans
  40. extern long                gSymmetry;        // the currently chosen symmetry group
  41.  
  42. // Proto for Gestalt function to check for GX
  43. Boolean QuickDrawGXInstalled(void);
  44.  
  45. Boolean TileInit(void)
  46. {
  47.     unsigned long    seed;
  48.     MenuHandle        mhndl;
  49.     
  50.     gxViewPort    windPort;
  51.  
  52.     // Offscreen stuff
  53.     CTabHandle    standardClut;
  54.     gxBitmap    offMap;
  55.     gxShape        offBits;
  56.     
  57.     // Check for GX
  58.     if(QuickDrawGXInstalled() == false)
  59.     {
  60.         SysBeep(10);
  61.         ExitToShell();
  62.     }
  63.     
  64.     /**     
  65.         Define the graphics heap size. If you do not make this call, graphics will
  66.         create this heap automatically. How? It will create a heap which is a % of your
  67.         application's ideal memory foot print. this call allows you to explicity define 
  68.         the ammount of memory used by the graphics system for it's graphics objects heap.
  69.     **/
  70.     gGraphicsClient = GXNewGraphicsClient(nil, 0L, 0L);
  71.     
  72.     /** 
  73.         Set kDebugging to TRUE, if you want to have graphics library errors & notices posted.  This
  74.         functionality will only work with the "debugging" Secret Graphics init.  If this
  75.         init is not installed, these functions will not work.
  76.     **/
  77.     if (kDebugging) {
  78.         SetGraphicsLibraryErrors ();
  79.         SetGraphicsLibraryNotices();    
  80.     }
  81.  
  82.     /** 
  83.         Set  "kgiveMeValidation" to TRUE, if you want run-time checking. The drawing will get very 
  84.         SLOW due to all of the internal checking. You can set the validation to a higher level,
  85.         see Chapter 3 for details. 
  86.     **/
  87.     if ( kGiveMeValidation ) GXSetValidation(gxPublicValidation); 
  88.  
  89.     /** initialize the new graphics environment **/
  90.     GXEnterGraphics();
  91.  
  92.     // Init the common color library
  93.     InitCommonColors();
  94.        
  95.        // Create the window.
  96.     gWindPtr = NewWindow(        &gWindRecord, 
  97.                                 &gWindowQDRect, 
  98.                                 "\pDrag Colored Shapes",
  99.                                 true, 
  100.                                 documentProc,
  101.                                   (WindowPtr)(-1), 
  102.                                   true,     /* has a close box */
  103.                                   0L    );
  104.     SetPort(gWindPtr);
  105.     ClipRect(&gWindPtr->portRect);
  106.     EraseRect(&gWindPtr->portRect);
  107.  
  108.     SetDefaultViewPort(GXNewWindowViewPort(gWindPtr));
  109.     
  110.     // Reset random number generator
  111.     GetDateTime(&seed);
  112.     randSeed = seed;
  113.     
  114.     /* Set up a color space with the default 8-bit colors for our offscreen */
  115.     standardClut = GetCTable(72);
  116.     gColors = CTableToColorSet( standardClut );
  117.     DisposCTable(standardClut);
  118.  
  119.     /* Set up the off-screen bitmap */
  120.     offMap.image = nil;
  121.     offMap.width = gWindPtr->portRect.right - gWindPtr->portRect.left;
  122.     offMap.height = gWindPtr->portRect.bottom - gWindPtr->portRect.top;
  123.     offMap.rowBytes = 0;
  124.     offMap.pixelSize = 8;
  125.     offMap.space = gxIndexedSpace;
  126.     offMap.set = gColors;
  127.     offMap.profile = nil;
  128.     offBits = GXNewBitmap(&offMap, &gOrigin);
  129.     CreateOffscreen(&gOffScreen, offBits);
  130.     GXDisposeShape(offBits);
  131.     
  132.     // set up background shape with the right color
  133.     gBGShape = GetWindowBoundsShape(gWindPtr);
  134.     SetShapeCommonColor(gBGShape, kBGColor);
  135.  
  136.     // Set up our patterned shape (DefaultTileAndPattern will actually add the pattern to it)
  137.     gGridShape = GXCopyToShape(nil, gBGShape);
  138.     SetShapeCommonColor(gGridShape, kGridColor);
  139.     
  140.     // Set them both to draw offscreen
  141.     GXSetShapeTransform(gBGShape, gOffScreen.xform);
  142.     GXSetShapeTransform(gGridShape, gOffScreen.xform);
  143.     
  144.     // Make the tile shape
  145.     gTileShape = GXNewShape(gxPolygonType);
  146.     GXSetShapeFill(gTileShape, gxOpenFrameFill);
  147.     
  148.     // Set up the dragger. It has one open polygon
  149.     gDragger.snake1 = GXNewShape(gxPolygonType);
  150.     GXSetShapeFill(gDragger.snake1, gxOpenFrameFill);
  151.     SetShapeCommonColor(gDragger.snake1, kDragColor);
  152.     GXSetShapePen(gDragger.snake1, ff(3));
  153.     
  154.     // Set up the initial pattern
  155.     gPattern.attributes = gxNoAttributes;
  156.     gPattern.pattern = gTileShape;
  157.     
  158.     // Set up all the initial geometry
  159.     DefaultTileAndPattern();
  160.     
  161.     // Add the "Tile" menu
  162.     mhndl = GetMenu(kTileMenuID);
  163.     if(mhndl == nil)
  164.         return false;
  165.  
  166.     (*mhndl)->menuID = kTileMenuID;
  167.     InsertMenu(mhndl, 0);
  168.     gAppMenuHandle = mhndl;
  169.     
  170.     /* Update the menu bar */
  171.     DrawMenuBar();
  172.  
  173.     return(true);
  174. }
  175.  
  176. void    TileQuit(void)
  177. {
  178.     // Tile stuff
  179.     DisposeOffscreen(&gOffScreen);
  180.     GXDisposeColorSet(gColors);
  181.     GXDisposeShape(gTileShape);
  182.     GXDisposeShape(gGridShape);
  183.     GXDisposeShape(gBGShape);
  184.     DisposeDragger();
  185.     DisposeOpShapes();
  186.     
  187.     GXDisposeViewPort(GXGetWindowViewPort(gWindPtr));
  188.     if(gWindPtr != nil)
  189.         CloseWindow(gWindPtr);
  190.     
  191.     DisposeCommonColors();
  192. }
  193.  
  194. Boolean QuickDrawGXInstalled(void)
  195. {
  196.     long    theFeatureInQuestion;
  197.     
  198.     //    Check for GX
  199.     if (Gestalt('grfx', &theFeatureInQuestion) == noErr)
  200.         return(true);
  201.     return(false);
  202. }
  203.  
  204.     
  205.  
  206.